package com.hmall.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.constatnts.MqConstants;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageDTO;
import com.hmall.item.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public void updateItem(Long id, Integer stock) {
        itemMapper.updateItemNum(id, stock);

    }

    //分页查询
    @Override
    public PageDTO<Item> getPageItem(Integer curpage, Integer size) {
        Page<Item> page = new Page<>(curpage, size);

        LambdaQueryWrapper<Item> wrapper = Wrappers.lambdaQuery(Item.class);
        Page<Item> itemPage = itemMapper.selectPage(page, wrapper);

        List<Item> list = itemPage.getRecords();


        return new PageDTO<>(itemPage.getTotal(), list);

    }

    /**
     * 添加商品
     *
     * @param item
     */
    @Override
    public void addItemShop(Item item) {
        int insert = itemMapper.insert(item);


    }

    /**
     * 修改商品状态
     */

    @Override
    public void reviseItemStatus(Long id, Integer status) {
        if (status == 1) {
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, id);
            log.info("成功发送商品上架消息 " + id);
        }
        if (status == 2) {
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_DELETE_KEY, id);
            log.info("成功发送商品下架消息 " + id);
        }
        Item item = itemMapper.selectById(id);

        item.setStatus(status);
        itemMapper.updateById(item);

    }

    /**
     * 修改商品
     */
    @Override
    public void updateItemById(Item item) {

        this.saveOrUpdate(item);

    }

    @Override
    public void deleteItemById(Long id) {
        Item item = itemMapper.selectById(id);
        if (item.getStatus() == 2) {
            this.removeById(id);
        }
    }


    //根据id查找数据
    @Override
    public Item selectItemById(Long id) {
        return this.getById(id);
    }

    @Override
    public void reduceGoodsFrequency(Long itemId, Integer num) {
        itemMapper.reduceGoodsFrequency(itemId, num);
    }

    @Override
    public void updateStock(Long id, Integer num) {
        //通过商品id查询商品信息
        Item item = getById(id);
        //将库存加上
        Integer stock = item.getStock();

        //更新库存
        Integer newStock = num + stock;
        item.setStock(newStock);

        this.updateItemById(item);
    }
}



